﻿using SalesSys.BLL;
using SalesSys.DAL.Models;
using SalesSys.WebCore;
using SalesSys.WebCore.Authorize;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using SalesSys.Comm.Utility;

namespace SalesSys.Site.Controllers.Api
{
    /// <summary>
    /// 信息收集
    /// </summary>
    [RoutePrefix("api/InfoCollection")]
    public class InfoCollectionController : BaseApiController
    {
        InfoCollectionBLL infoBLL = BllFactory.GetBll<InfoCollectionBLL>();

        /// <summary>
        /// 钢厂信息收集列表
        /// </summary>
        /// <param name="name">模糊查询</param>
        /// <param name="type">类型</param>
        /// <param name="dt">查询时间</param>
        /// <param name="page">页码</param>
        /// <param name="rows">每页条数</param>
        /// <returns></returns>
        [Route("List")]
        public ViewDtos<InfoCollection> Get(string name = null, string type = null, DateTime? dt = null, int page = 1, int rows = 10)
        {
            var data = infoBLL.GetInfo(name, type, dt, page, rows);
            var dto = ViewDto.CreateList(data.Item2);
            dto.Rows = rows;
            dto.Page = page;
            dto.Total = data.Item1;
            return dto;
        }

        /// <summary>
        /// 获取制定钢厂时间，信息收集信息
        /// </summary>
        /// <param name="SupplierName">钢厂名称</param>
        /// <param name="dt">查询时间</param>
        /// <returns></returns>
        [Route("GetInfo")]
        public IResultState<InfoCollection[]> GetInfo(string SupplierName, DateTime dt)
        {
            if (string.IsNullOrWhiteSpace(SupplierName))
            {
                return new ResultState<InfoCollection[]>() { Msg = "钢厂名称不能为空", State = (int)ReState.ArgError };
            }
            var data = infoBLL.Get(SupplierName, dt);
            return ReState.Success.CreateResult("", data);
        }

        /// <summary>
        /// 新增钢厂信息
        /// </summary>
        /// <param name="vehicleInfos">钢厂信息（数组）</param>
        [Route("Add")]
        public void Post(InfoCollection[] vehicleInfos)
        {
            foreach (var vehicleInfo in vehicleInfos)
            {
                vehicleInfo.CreaterId = ClientHelper.ClientInfo.Id;
                vehicleInfo.UpdateTime = DateTime.Now;
            }

            infoBLL.Create(vehicleInfos);
        }
        /// <summary>
        /// 获取驻厂所管理钢厂供应商名称
        /// </summary>
        /// <returns>钢厂名称（数组）</returns>
        [Route("GetFieldManProviders")]
        public string[] GetFieldManProviders()
        {
            return infoBLL.GetFieldManProviders(ClientHelper.ClientInfo.Id).Where(p => p.ProviderType == ProviderType.Producer).Select(p => p.Name).ToArray();
        }

        /// <summary>
        /// 修改钢厂信息
        /// </summary>
        /// <param name="vehicleInfo">信息实体</param>
        [Route("Edit")]
        public void Put(InfoCollection vehicleInfo)
        {
            var old = infoBLL.Details(vehicleInfo.Id);
            old.InfoDate = vehicleInfo.InfoDate;
            old.Name = vehicleInfo.Name;
            old.Price = vehicleInfo.Price;
            old.Stock = vehicleInfo.Stock;
            old.SupplierName = vehicleInfo.SupplierName;
            old.Type = vehicleInfo.Type;
            old.UpdateTime = DateTime.Now;
            old.Volume = vehicleInfo.Volume;
            infoBLL.Edit(old);
        }

        /// <summary>
        /// 设置某天，某钢厂的记录信息，删除旧的
        /// </summary>
        /// <param name="vehicleInfos">钢厂信息</param>
        /// <param name="dt">日期</param>
        [Route("SetInfoByReplace")]

        public void PostSetInfoByReplace(InfoCollection[] vehicleInfos, DateTime? dt = null)
        {
            if (vehicleInfos == null || vehicleInfos.Length < 1)
            {
                return;
            }
            if (dt == null)
            {
                dt = DateTime.Now;
            }
            infoBLL.SetProviderInfoByReplace(vehicleInfos, ClientHelper.ClientInfo.Id, dt.Value);
        }
        /// <summary>
        /// 查询出货量
        /// </summary>
        /// <param name="begin">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <returns></returns>
        [Route("SelectVolume")]
        [HttpGet]
        public string[][] SelectVolume(DateTime begin, DateTime end)
        {
            var data = infoBLL.SelectVolume(begin, end);
            var list = new List<string[]>();
            list.Add(new[] { "钢厂", "出货量" });
            list.AddRange(data);
            return list.ToArray();
        }
        /// <summary>
        /// 获取库存统计
        /// </summary>
        /// <param name="begin">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <returns></returns>
        [HttpGet]
        [Route("SelectStock")]

        public string[][] SelectStock(DateTime begin, DateTime end)
        {
            return infoBLL.SelectStock(begin, end);
        }
    }
}
